<?php

defined('BASEPATH') or die('forbidden');

class SealAccountList_Controller Extends Controller {

    private $wholeServerHost;	//全局地址
	private $areaList;	//区号列表 
	private $sealAccountDesribeList;	//封角色描述列表
	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $config = get_config();
        
        //得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        
        $this->wholeServerHost = $config['host_url'];
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrID']] = $row['SvrID']."-".$row['SvrName'];
        }

	    
	    //封号描述
        $sql = "SELECT *  FROM gm_sealaccount ";
        $this->db->query($sql);
    	while ($n = $this->db->get_array('default')) 
    	{
            $this->sealAccountDesribeList[$n['accountid']]['description']=$n['description'];  
            $this->sealAccountDesribeList[$n['accountid']]['areanum']=$n['areanum'];  
            $this->sealAccountDesribeList[$n['accountid']]['rolename']=$n['rolename'];   
        } 
    }

    function index() {
        $this->view->display("seal/sealaccountlist/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $result = array(
            'page' => $page,
            'total' => 0,
            'rows' => null
        );
        $str = $this->wholeServerHost . "action=13";

        $strResult = vpost($str);
        if ($strResult != '0' || $strResult === FALSE)
            $result = 1;
        $row = json_decode($strResult, true);
        if ($row != null) {
            foreach ($row as $key => $value) 
            {
                if ($value['seal_end_time'] <= time())
                {
                    continue;
                }
                $value['seal_end_time'] = get_datetime($value['seal_end_time']);
                $value['description'] = $this->sealAccountDesribeList[$value['account_id']]['description'];
                $value['areanum'] = $this->sealAccountDesribeList[$value['account_id']]['areanum'];
                $value['rolename'] = $this->sealAccountDesribeList[$value['account_id']]['rolename'];
                $rows[] = array('id' => $value['account_id'], 'cell' => $value);
            }
            $total = count($rows);
            $result = array(
                'page' => $page,
                'total' => $total,
                'rows' => $rows
            );
        }
        echo json_encode($result);
    }

    function add() {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            if (empty($rowdata['area']) || empty($rowdata['role_name']))
                echo "<script>alert('area or role_name is null!!!')</script>";

		    $post_data['ssionID']='';
		    $post_data['SessionKey']='';
		    $post_data['PlayerID']=(int)$rowdata['accountid'];
		    $post_data['SvrID']=(int)$rowdata['area'];
		    $post_data['RoleName']=(string)$rowdata['role_name'];
		    $post_data['Enable']=(int)$rowdata['enable'];
		    $post_data['DisableDay']=(int)$rowdata['seal_time'];

            $strResult=vpost($this->wholeServerHost.'gm_enable_account',3,json_encode($post_data));
            $result=json_decode($strResult,true);
            //操作失败或者连接失败
            if ($result['RetCode'] === 0) {
                insert_log(translate('MENU_SEAL_SEALACCOUNTLIST'),translate('ADD'), NULL, $rowdata);
                $this->db->insert("gm_sealaccount",array('description'=>$rowdata['description'],'accountid'=>$rowdata['accountid'],
                    'areanum'=>$rowdata['area'],'rolename'=>$rowdata['role_name']));
                echo " seal account success ";
                cp_tips("success");

            } else
            {
                echo " connct error ";
            }

        }else{
	        $time = array("86400" => translate('ONE_DAY'), "172800" => translate('TWO_DAYS'), "604800" => translate('ONE_WEEK'), "0" => translate('PERPETUAL'));
	        $this->view->assign("timelist", $this->form->select("row[seal_time]", $time, '', array('id' => 'seal_time')));
	        $this->view->assign("arealist", $this->areaList);
	        $this->view->display("seal/sealaccountlist/add");
        }
    }

    function del($id = 0) {
        $post_data['ssionID']='';
        $strResult=vpost($this->wholeServerHost.'gm_enable_account',3,json_encode($post_data));
        $str = $this->wholeServerHost . "action=14&id={$id}";

        $strResult = vpost($str);

        $res = (int) $strResult == 0 ? 1 : 0;
        if ($res == 1) {
            insert_log(translate('MENU_SEAL_SEALACCOUNTLIST'),translate('DEL'), $id);
        }
        json_output($res, translate('C_TIPS_ERROR_TEXT'));
    }

    //通过区号与角色名得到账号id
    function mygetAccountId($areanum, $rolename)
    {
        $post_url='';
        $server_list=get_server_list();
        foreach ($server_list as $k=>$v){
            if($v['SvrID'] == $areanum){
                $post_url=$v['SvrOutAddr'];
            }
        }
        $post_data='{"Name":"'.$rolename.'"}';
        $row = vpost($post_url.'/query_account_id',3,$post_data);
        $rows=json_decode($row,true);
        return $rows;
    }
}